﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Globalization;
using System.Drawing;

namespace KokosSearcher
{
	public class KokosAuction
	{
		private string _xml = "";

		private int _id, _period, _timeToEnd;
		private decimal _value, _percent,_paid;
		private string _title,_intent,_description, _username, _auctionLink;
		private DateTime _finishDate;
		private AuctionUser _user = null;
		private List<Payback> paybackList = new List<Payback>();
		private XmlElement xmlElement;
		private XmlNode xmlNode;
		Image _rating;

		public int Id
		{
			get { return this._id; }
			set { this._id = value; }
		}

		public int Period
		{
			get { return this._period; }
			set { this._period = value; }
		}

		//public int TimeToEnd
		//{
		//    get { return this._timeToEnd; }
		//    set { this._timeToEnd = value; }
		//}

		public string TimeToEnd
		{
			get { return this.getTimeToEndInHumanForm(); }
		}

//		public int Rating
//		{
//			get { return this._rating; }
//			set { this._rating = value; }
//		}

		public Image Rating
		{
			get { return this._rating; }
			set { this._rating = value; }
		}

		public decimal Value
		{
			get { return this._value; }
			set { this._value = value; }
		}
		
		public decimal Percent
		{
			get { return this._percent; }
			set { this._percent = value; }
		}

		public decimal Paid
		{
			get { return this._paid; }
			set { this._paid = value; }
		}

		public string Title
		{
			get { return this._title; }
			set { this._title = value; }
		}

		public string Intent
		{
			get { return this._intent; }
			set { this._intent = value; }
		}

		public string Description
		{
			get { return this._description; }
			set { this._description = value; }
		}

		public string UserName
		{
			get { return this._username; }
		}

		public DateTime FinishDate
		{
			get { return this._finishDate; }
			set { this._finishDate = value; }
		}

		public List<Payback> PaybackCollection
		{
			get { return this.paybackList; }
			set { this.paybackList = value; }
		}

		public AuctionUser User
		{
			get { return this._user; }
			set { this._user = value; }
		}
		
		public string AuctionLink
		{
			get {
				if (!String.IsNullOrEmpty(this._id.ToString()))
				{
					return "https://kokos.pl/aukcje?id=" + this._id.ToString();
				}
				else
				{
					return "";
				}
			}
		}

		public KokosAuction(string p_xml)
		{
			if (string.IsNullOrEmpty(p_xml))
			{
				throw new Exception("Niepoprawne wywołanie konstruktora");
			}

			this._xml = p_xml;

			this.loadAuctionData();
		}

		public KokosAuction(XmlElement xmlElement)
		{
			this.xmlElement = xmlElement;

			try
			{
				// odczytanie statusu aukcji
				int statusAukcji = Convert.ToInt32(this.xmlElement.SelectSingleNode("status").InnerText);

				// interesują nas tylko aukcje już trwające (ze statusem powyżej 110)
				StatusAukcji Status = new StatusAukcji(statusAukcji);

				if (Status.Trwajaca)
				{
					this._id = Convert.ToInt32(this.xmlElement.SelectSingleNode("id").InnerText);
					this._username = this.xmlElement.SelectSingleNode("user").InnerText;
					this._period = Convert.ToInt32(this.xmlElement.SelectSingleNode("period").InnerText);
					this._timeToEnd = Convert.ToInt32(this.xmlElement.SelectSingleNode("timeToEnd").InnerText);
					this._value = Convert.ToDecimal(this.xmlElement.SelectSingleNode("value").InnerText, CultureInfo.InvariantCulture.NumberFormat);
					this._percent = Convert.ToDecimal(this.xmlElement.SelectSingleNode("percent").InnerText, CultureInfo.InvariantCulture.NumberFormat);
					this._paid = Convert.ToDecimal(this.xmlElement.SelectSingleNode("paid").InnerText, CultureInfo.InvariantCulture.NumberFormat);
					this._title = this.xmlElement.SelectSingleNode("title").InnerText;
					this._intent = this.xmlElement.SelectSingleNode("intent").InnerText;
					//this._rating = Convert.ToInt32(this.xmlElement.SelectSingleNode("rating").InnerText);
					this._rating = new Rating().getRatingImage(Convert.ToInt32(this.xmlElement.SelectSingleNode("rating").InnerText));
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private string getTimeToEndInHumanForm()
		{
			string sTimeToEnd = "";

			if (this._timeToEnd > 0)
			{
				// obliczenie ilości dni, godzin i minut do zakończenia aukcji
				int dni, godziny, minuty;

				dni = 0;
				godziny = 0;
				minuty = 0;

				dni = Convert.ToInt16(this._timeToEnd / 60 / 24);
				godziny = Convert.ToInt16((this._timeToEnd - (dni * 24 * 60)) / 60);
				minuty = this._timeToEnd - (dni * 24 * 60) - (godziny * 60);

				if (dni > 0)
				{
					sTimeToEnd += dni;

					if (dni == 1)
					{
						sTimeToEnd += " dzień ";
					}
					else
					{
						sTimeToEnd += " dni ";
					}
				}

				if (godziny > 0)
				{
					sTimeToEnd += godziny + " godz. ";
				}

				if (minuty > 0)
				{
					sTimeToEnd += minuty + " min.";
				}
			}
			else
			{
				sTimeToEnd = "Aukcja zakończona";
			}

			return sTimeToEnd;
		}

		private void loadAuctionData()
		{
			try
			{
				XmlDocument xmlDoc = new XmlDocument();
				xmlDoc.LoadXml(this._xml);

				this._id = Convert.ToInt32(xmlDoc.DocumentElement.SelectSingleNode("//response/auction/id").InnerText);
				this._user = new AuctionUser();

				// Gdy aukcja jest w statusie 110 - jest dopiero tworzona, to przesyłane jest tylko Id i status aukcji
				if (Convert.ToInt32(xmlDoc.DocumentElement.SelectSingleNode("//response/auction/status").InnerText) != 110)
				{
					this._period = Convert.ToInt32(xmlDoc.DocumentElement.SelectSingleNode("//response/auction/period").InnerText);
					this._timeToEnd = Convert.ToInt32(xmlDoc.DocumentElement.SelectSingleNode("//response/auction/timeToEnd").InnerText);
					this._value = Convert.ToDecimal(xmlDoc.DocumentElement.SelectSingleNode("//response/auction/value").InnerText, CultureInfo.InvariantCulture.NumberFormat);
					this._percent = Convert.ToDecimal(xmlDoc.DocumentElement.SelectSingleNode("//response/auction/percent").InnerText, CultureInfo.InvariantCulture.NumberFormat);
					this._paid = Convert.ToDecimal(xmlDoc.DocumentElement.SelectSingleNode("//response/auction/paid").InnerText, CultureInfo.InvariantCulture.NumberFormat);
					this._title = xmlDoc.DocumentElement.SelectSingleNode("//response/auction/title").InnerText;
					this._intent = xmlDoc.DocumentElement.SelectSingleNode("//response/auction/intent").InnerText;
					this._description = xmlDoc.DocumentElement.SelectSingleNode("//response/auction/description1").InnerText + "\n" +
											xmlDoc.DocumentElement.SelectSingleNode("//response/auction/description2").InnerText + "\n" +
											xmlDoc.DocumentElement.SelectSingleNode("//response/auction/description3").InnerText;
					this._finishDate = Convert.ToDateTime(xmlDoc.DocumentElement.SelectSingleNode("//response/auction/finishDate").InnerText);

					this._user = new AuctionUser(xmlDoc);

					foreach (XmlElement xmlElement in xmlDoc.SelectNodes("//response/auction/paybackSchedule/payback"))
					{
						this.paybackList.Add(new Payback(xmlElement));
					}
				}

			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

	} // class KokosAuction

	public class Payback
	{
		private DateTime _scheduleDate, _paidDate;
		private decimal _capital, _interest, _installment;

		public DateTime ScheduleDate
		{
			get { return this._scheduleDate; }
			set { this._scheduleDate = value; }
		}

		public DateTime PaidDate
		{
			get { return this._paidDate; }
			set { this._paidDate = value; }
		}

		public decimal Capital
		{
			get { return this._capital; }
			set { this._capital = value; }
		}

		public decimal Interest
		{
			get { return this._interest; }
			set { this._interest = value; }
		}

		public decimal Installment
		{
			get { return this._installment; }
			set { this._installment = value; }
		}

		public Payback(XmlElement p_xmlElement)
		{
			this._scheduleDate	= Convert.ToDateTime(p_xmlElement.GetElementsByTagName("scheduleDate").Item(0).InnerText);
			this._paidDate		= String.IsNullOrEmpty(p_xmlElement.GetElementsByTagName("paidDate").Item(0).InnerText) ? new DateTime(1890,01,01) : Convert.ToDateTime(p_xmlElement.GetElementsByTagName("paidDate").Item(0).InnerText);
			this._capital		= Convert.ToDecimal(p_xmlElement.GetElementsByTagName("capital").Item(0).InnerText, CultureInfo.InvariantCulture.NumberFormat);
			this._interest		= Convert.ToDecimal(p_xmlElement.GetElementsByTagName("interest").Item(0).InnerText, CultureInfo.InvariantCulture.NumberFormat);
			this._installment	= Convert.ToDecimal(p_xmlElement.GetElementsByTagName("installment").Item(0).InnerText, CultureInfo.InvariantCulture.NumberFormat);
		}
	} // Payback

	public class AuctionUser
	{
		private XmlDocument xmlDoc;

		private int _id, _rating;
		private string _user;
		private decimal _income, _expenses, _credits;

		public int Id
		{
			get { return this._id; }
			set { this._id = value; }
		}

		public int Rating
		{
			get { return this._rating; }
			set { this._rating = value; }
		}

		public string User
		{
			get { return this._user; }
			set { this._user = value; }
		}

		public decimal Income
		{
			get { return this._income; }
			set { this._income = value; }
		}

		public decimal Expenses
		{
			get { return this._expenses; }
			set { this._expenses = value; }
		}

		public decimal Credits
		{
			get { return this._credits; }
			set { this._credits = value; }
		}

		public AuctionUser(XmlDocument p_xmlDoc)
		{
			this.xmlDoc = p_xmlDoc;

			this._id = Convert.ToInt32(this.xmlDoc.DocumentElement.SelectSingleNode("//response/auction/PB/userId").InnerText);
			this._rating = Convert.ToInt32(this.xmlDoc.DocumentElement.SelectSingleNode("//response/auction/PB/rating").InnerText);
			this._user = this.xmlDoc.DocumentElement.SelectSingleNode("//response/auction/PB/user").InnerText;
			this._income = Convert.ToDecimal(this.xmlDoc.DocumentElement.SelectSingleNode("//response/auction/PB/income").InnerText, CultureInfo.InvariantCulture.NumberFormat);
			this._expenses = Convert.ToDecimal(this.xmlDoc.DocumentElement.SelectSingleNode("//response/auction/PB/expenses").InnerText, CultureInfo.InvariantCulture.NumberFormat);
			this._credits = Convert.ToDecimal(this.xmlDoc.DocumentElement.SelectSingleNode("//response/auction/PB/credits").InnerText, CultureInfo.InvariantCulture.NumberFormat);
		}

		public AuctionUser()
		{
			
		}
	} // class AuctionUser
}
